import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;

// Press ⇧ twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {

    public static int maxRandomLines1 = 10;
    public static int maxRandomLines2 = 10;
    public static int maxRandomLines3 = 20;

    public static void main(String[] args) {
        while (true) {
            if (!extracted()) {
                break;
            }
        }
    }

    private static boolean extracted() {
        String string = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nWhat's your string?  ");
        try {
            string = br.readLine();
            if (string.equals("exit()")) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        int pad = new Random().nextInt(1000);

        int padArgus = 0xFFFF;
        int[] ints = new int[string.length()];

        for (int i = 0; i < string.length(); i++) {
            char a = string.charAt(i);
            int b = ~((int) a ^ padArgus) - pad;
            ints[i] = b;
        }

        StringBuilder s = new StringBuilder();
        for (int anInt : ints) {
            int a = anInt;
            a += pad;
            s.append((char) (a & padArgus));
        }
        System.out.println("测试加密解密成功：" + s);
        System.out.println("开始生成代码:\n");

        StringBuilder sb = new StringBuilder();

        String random2hidePadArgusName = getRandomString();
        String random2hidePadArgusSubName = getRandomString();
        String padArgusName = getRandomString();
        String argsArrayName = getRandomString();
        String intsArrayName = getRandomString();
        String resultStringName = getRandomString();
        String indexStringName = getRandomString();
        String itemStringName = getRandomString();
        String zeroStrName = getRandomString();
        String zeroIndexName = getRandomString();
        String oneIndexName = getRandomString();
        String oneRealIndexName = getRandomString();
        String twoIndexName = getRandomString();
        String twoRealIndexName = getRandomString();
        String zeroRealIndexName = getRandomString();

        int random2hidePadArgus = new Random().nextInt(padArgus);
        sb.append("int ").append(random2hidePadArgusName).append(" = ").append(random2hidePadArgus).append(";");
        sb.append("\n");
        sb.append("int ").append(random2hidePadArgusSubName).append(" = ").append(padArgus - random2hidePadArgus).append(";");
        sb.append("\n");
        sb.append("int ").append(padArgusName).append(" = ").append(random2hidePadArgusName).append(" + ").append(random2hidePadArgusSubName).append(";");
        sb.append("\n");

        // 0
        String leftZeroStr = getRandomString();
        int zeroIndex = leftZeroStr.length();
        String zeroStr = leftZeroStr + "0" + getRandomString();
//        Integer.parseInt(String.valueOf(zeroStr.charAt(zeroIndex)));

        sb.append("int ").append(zeroIndexName).append(" = ").append(zeroIndex).append(";");
        sb.append("\n");

        // 1
        int oneIndex = zeroStr.length();
        zeroStr = zeroStr + "1" + getRandomString();
        sb.append("int ").append(oneIndexName).append(" = ").append(oneIndex).append(";");
        sb.append("\n");
        // 2
        int twoIndex = zeroStr.length();
        zeroStr = zeroStr + "2" + getRandomString();
        sb.append("int ").append(twoIndexName).append(" = ").append(twoIndex).append(";");
        sb.append("\n");

        sb.append("String ").append(zeroStrName).append(" = \"").append(zeroStr).append("\";");
        sb.append("\n");
        sb.append("int ").append(zeroRealIndexName).append(" = ").append("Integer.parseInt(String.valueOf(").append(zeroStrName).append(".charAt(").append(zeroIndexName).append(")));");
        sb.append("\n");
        sb.append("int ").append(oneRealIndexName).append(" = ").append("Integer.parseInt(String.valueOf(").append(zeroStrName).append(".charAt(").append(oneIndexName).append(")));");
        sb.append("\n");
        sb.append("int ").append(twoRealIndexName).append(" = ").append("Integer.parseInt(String.valueOf(").append(zeroStrName).append(".charAt(").append(twoIndexName).append(")));");
        sb.append("\n");

        sb.append("int ").append(argsArrayName).append("[] = ").append("{").append(pad).append(",").append(padArgusName).append(",").append(zeroRealIndexName);
        for (int i = 0; i < new Random().nextInt(maxRandomLines1); i++) {
            sb.append(",").append(new Random().nextInt(Integer.MAX_VALUE));
        }
        sb.append("};");
        sb.append("\n");

        sb.append("int ").append(intsArrayName).append("[] = ").append("{");
        for (int i = 0; i < ints.length; i++) {
//            sb.append("0x").append(Integer.toHexString(ints[i]));
            sb.append(ints[i]);
            if (i < ints.length - 1) {
                sb.append(", ");
            }
        }
        sb.append("};");
        sb.append("\n");

        sb.append("String ").append(resultStringName).append(" = \"\";");
        sb.append("\n");
        int lineRandom = new Random().nextInt(4);
        sb.append("for (int ").append(indexStringName).append(" = ").append(argsArrayName).append("[").append(twoRealIndexName).append("]").append("; ").append(indexStringName).append(" < ").append(intsArrayName).append(".length; ").append(indexStringName).append("++) {");
        sb.append("\n");
        if (lineRandom == 0) {
            sb.append(getRandomCode());
        }
        sb.append("    ").append("int ").append(itemStringName).append(" = ").append(intsArrayName).append("[").append(indexStringName).append("];");
        sb.append("\n");
        if (lineRandom == 1) {
            sb.append(getRandomCode());
        }
        sb.append("    ").append(itemStringName).append(" += ").append(argsArrayName).append("[").append(zeroRealIndexName).append("]").append(";");
        sb.append("\n");
        if (lineRandom == 2) {
            sb.append(getRandomCode());
        }
        sb.append("    ").append(resultStringName).append(" += ").append("(char)(").append(itemStringName).append(" & ").append(argsArrayName).append("[").append(oneRealIndexName).append("]").append(");");
        sb.append("\n");
        if (lineRandom == 3) {
            sb.append(getRandomCode());
        }
        sb.append("}");
        sb.append("\n");
        sb.append("//==>").append(string).append("<==");
        sb.append("\n");
        sb.append("String resultString = ").append(resultStringName).append(";");
        sb.append("\n");
        System.out.println(sb);

        StringSelection stringSelection = new StringSelection(sb.toString());
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(stringSelection, null);
        System.out.println("===========内容已复制到剪切板===========");
        System.out.println("==>"+ string +"<==");
        return true;
    }

    public static StringBuilder getRandomCode() {
        StringBuilder sb = new StringBuilder();
        String str1Name = getRandomString();
        String str2Name = getRandomString();
        String str3Name = getRandomString();
        String str4Name = getRandomString();
        sb.append("\n");
        String str1Value = getRandomString();
        sb.append("    String ").append(str1Name).append(" = \"").append(str1Value).append("\";");
        sb.append("\n");
        String str2Value = getRandomString();
        sb.append("    String ").append(str2Name).append(" = \"").append(str2Value).append("\";");
        sb.append("\n");
        sb.append("    String ").append(str3Name).append(" = ").append(str1Name).append(" + ").append(str2Name).append(";");
        sb.append("\n");
        sb.append("    String ").append(str4Name).append(" = \"\"").append(";");
        sb.append("\n");
        int r = new Random().nextInt(maxRandomLines2);
        for (int i = 0; i < r; i++) {
            String randomStrName = getRandomString();
            String randomStr = getRandomString();
            sb.append("    String ").append(randomStrName).append(" = \"").append(randomStr).append("\";");
            sb.append("\n");

            int randomIndex = new Random().nextInt(randomStr.length());
            sb.append("    ").append(str4Name).append(" = ").append(str4Name).append(" + ").append(randomStrName).append(".charAt(").append(randomIndex).append(");");
            sb.append("\n");
        }

        int strLength = (str1Value + str2Value).length() + r;

        sb.append("    if((").append(str3Name).append(" + ").append(str4Name).append(").length() > ").append(strLength).append("){");
        sb.append("\n");

        int rr = new Random().nextInt(maxRandomLines3);

        String cacheName = str4Name;

        for (int i = 0; i < rr; i++) {
            //int
            //long
            //boolean
            //String
            //double
            //float
            String lastRandomName = getRandomString();
            String type = "int";
            String typeValueOf = "Integer.valueOf";
            int typeRandom = new Random().nextInt(5);
            int subRandom = new Random().nextInt(8);
            String sub = "+"; // + - * / & ^ |
            if (typeRandom == 1) {
                type = "long";
                typeValueOf = "Long.valueOf";
            } else if (typeRandom == 2) {
                type = "int";
                typeValueOf = "Integer.valueOf";
            } else if (typeRandom == 3) {
                type = "double";
                typeValueOf = "Double.valueOf";
            } else if (typeRandom == 4) {
                type = "float";
                typeValueOf = "Float.valueOf";
            }

            if (subRandom == 1) {
                sub = "-"; // + - * / & ^ |
            } else if (subRandom == 2) {
                sub = "*"; // + - * / & ^ |
            } else if (subRandom == 3) {
                sub = "/"; // + - * / & ^ |
            } else if (subRandom == 4) {
                sub = "&"; // + - * / & ^ |
            } else if (subRandom == 5) {
                sub = "^"; // + - * / & ^ |
            } else if (subRandom == 6) {
                sub = "|"; // + - * / & ^ |
            } else if (subRandom == 7) {
                sub = "%"; // + - * / & ^ |
            }
            sb.append("        ").append(type).append(" ").append(lastRandomName).append(" = ")
                    .append(typeValueOf).append("(")
                    .append("((Integer.valueOf(String.valueOf(").append(cacheName).append(")))").append(sub)
                    .append(new Random().nextInt(Integer.MAX_VALUE))
                    .append(") + \"\")").append(";");
            cacheName = lastRandomName;
            sb.append("\n");


        }

        int fssdfe = new Random().nextInt(2);
        if (fssdfe == 0) {

            sb.append("        ").append("try {");
            sb.append("\n");
            sb.append("        ").append("    throw new java.lang.Throwable(String.valueOf(").append(cacheName).append("));");
            sb.append("\n");
            sb.append("        ").append("} catch (java.lang.Throwable e) {");
            sb.append("\n");
            sb.append("        ").append("    throw new java.lang.RuntimeException(e);");
            sb.append("\n");
            sb.append("        }");
        } else {
            sb.append("        android.util.Log.d(\"").append(getRandom15String()).append("\", String.valueOf(").append(cacheName).append("));");
        }
        sb.append("\n");
        sb.append("    }");
        sb.append("\n");

        return sb;
    }

    public static String getRandomString() {
        int length = new Random().nextInt(50);
        while (length < 5) {
            length = new Random().nextInt(50);
        }
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        sb.append(chars.charAt(random.nextInt(chars.length() - 10)));
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(chars.length());
            char c = chars.charAt(index);
            sb.append(c);
        }
        return sb.toString();
    }
    public static String getRandom15String() {
        int length = new Random().nextInt(15);
        ;
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        sb.append(chars.charAt(random.nextInt(chars.length() - 10)));
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(chars.length());
            char c = chars.charAt(index);
            sb.append(c);
        }
        return sb.toString();
    }
}